package cn.com.hongfu.controller;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.com.hongfu.model.Knowledge;
import cn.com.hongfu.service.KnowledgeService;
import cn.com.hongfu.utils.KeyGeneratorImpl;
@Controller
@RequestMapping("/knowledge")
public class KnowledgeController {
	@Autowired
	public KeyGeneratorImpl keyGeneratorImpl ;
	@Autowired
	public KnowledgeService knowledgeService ;
	/**
	 * 返回新增装修常识视图
	 * @return 
	 */
	@RequestMapping("/knowledgeAddUI")
	public String addKnowledgeUI(ModelMap modelMap){
		return "/knowledge/knowledgeAdd";
	}
	/**
	 * 新增装修常识
	 * @param file
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("knowledgeAdd")
	public String knowledgeAdd(HttpServletRequest request, ModelMap model){
		Knowledge bean = new Knowledge();
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String coverPhoto = request.getParameter("coverPhoto");
        String src = coverPhoto.replaceAll("\"", "#").split("#")[1];
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        bean.setTitle(title);
        bean.setContent(content);
        bean.setCoverPhoto(src);
        bean.setCreateDate(sd.format(new Date()));
        knowledgeService.saveKnowledge(bean);
        return "redirect:/knowledge/knowledgeList.html";
	}
	/**
	 * 返回所有的装修常识
	 * @return 所有装修常识
	 */
	@RequestMapping("/knowledgeList")
	public String addKnowledgeList(ModelMap model){
		List<Knowledge> knowledges = knowledgeService.getAllKnowledge();
		model.addAttribute("knowledges", knowledges);
		return "/knowledge/knowledgeList";
	}
	@RequestMapping("/editUI")
	public String EditUI(ModelMap modelMap,HttpServletRequest request){
		modelMap.addAttribute("k", knowledgeService.getKnowledgeById(Integer.parseInt(request.getParameter("id"))));
		return "/knowledge/knowledgeEdit";
	}
	@RequestMapping("/edit")
	public String Edit(ModelMap modelMap,Knowledge knowledge){
		if(StringUtils.isNotEmpty(knowledge.getCoverPhoto()))
		knowledge.setCoverPhoto(knowledge.getCoverPhoto().replaceAll("\"", "#").split("#")[1]);
		knowledgeService.updateKnowledge(knowledge);
		return "redirect:/knowledge/knowledgeList.html";
	}
	@RequestMapping("/del")
	public String del(HttpServletRequest request){
		knowledgeService.delKnowledge(Integer.parseInt(request.getParameter("id")));
		return "redirect:/knowledge/knowledgeList.html";
	}
}
